package fr.lelouet.server.perf.snapshot.receivers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.lelouet.server.perf.snapshot.ActivityReport;
import fr.lelouet.server.perf.snapshot.HVSnapshot;
import fr.lelouet.server.perf.snapshot.SnapshotReceiver;

/**
 * loads a set of {@link HVSnapshot} from one hypervisor, then presents the data
 * and the set of events .
 */
public class TableLoader implements SnapshotReceiver {

	private static final Logger logger = LoggerFactory
			.getLogger(CSVExporter.class);

	public static final String CONSUMPTIONKEY = "consumption";

	public static final String TIMEKEY = "time";

	public final List<String> defaultEvents = Arrays.asList(new String[] {
			TIMEKEY, CONSUMPTIONKEY });

	@Override
	public void configure(Properties prop) {
		reset();
	}

	public void reset() {
		events.clear();
		events.addAll(defaultEvents);
		loadedData.clear();
	}

	@Override
	public void hypervisorModification(HVSnapshot toShow, String name) {
		Map<String, Double> data = toShow.unfold();
		data.put(TIMEKEY, (double) toShow.getDate());
		logger.info("snapshot {} folded into {}", toShow, data);
		loadedData.add(data);
		events.addAll(data.keySet());
	}

	@Override
	public void resourceModification(ActivityReport snapshot, String name) {
	}

	/** every data loaded and computed */
	protected List<Map<String, Double>> loadedData = new ArrayList<Map<String, Double>>();

	/** every event from the data loaded */
	protected LinkedHashSet<String> events = new LinkedHashSet<String>();

	/**
	 * @return the list of resources usages monitored. assumed to be on only one
	 *         hypervisor
	 */
	public List<Map<String, Double>> getLoadedData() {
		return loadedData;
	}

	public LinkedHashSet<String> getEvents() {
		return events;
	}

}
